import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:mall_ad/mall_ad.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: SplashPage(),
    );
  }
}

class SplashPage extends StatefulWidget {
  @override
  _SplashPageState createState() => _SplashPageState();
}

class _SplashPageState extends State<SplashPage>
    implements MallAdViewCallbacksHandler {
  @override
  void initState() {
    super.initState();
    SystemChrome.setEnabledSystemUIOverlays(
      [SystemUiOverlay.bottom],
    );
  }

  @override
  void dispose() {
    super.dispose();
    SystemChrome.setEnabledSystemUIOverlays(
      [SystemUiOverlay.top, SystemUiOverlay.bottom],
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: MallAdView(
        callback: (controller) {
          controller.fetchAd();
        },
        handler: this,
      ),
    );
  }

  @override
  void onJumpClicked() {
    _mainPage();
  }

  @override
  void onSplashAdDismiss() {
    _mainPage();
  }

  void _mainPage() {
    Navigator.of(context).pushReplacement(
      MaterialPageRoute(
        builder: (context) {
          return MainPage();
        },
      ),
    );
  }
}

class MainPage extends StatefulWidget {
  @override
  _MainPageState createState() => _MainPageState();
}

class _MainPageState extends State<MainPage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("主页"),
      ),
      body: Center(
        child: Text("内容"),
      ),
    );
  }
}
